package com.parkcar;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import HttpUtils.HttpCarRequest;
import util.BaseActivity;
import util.BaseTools;
import util.Constrants;

/**
 * Created by Administrator on 2016/2/19.
 */
public class AddCarActivity extends BaseActivity{
    private TextView left_num;
    private EditText right_num;
    private PopupWindow mStringPop,mCharPop;
    private LinearLayout activity_ll;
    private String first="浙";
    private HttpCarRequest carRequest;
    @Override
    protected void setUpui(Bundle arg0) {
        setContentView(R.layout.activity_add_car);
        setTitle("添加车辆");
        activity_ll = (LinearLayout) findViewById(R.id.activity_ll);
        left_num = (TextView) findViewById(R.id.left_num);
        right_num = (EditText) findViewById(R.id.right_num);
    }

    @Override
    protected void updateUI(boolean isLogin) {

    }
    public void submit(View view){
        String number = left_num.getText().toString()+right_num.getText().toString();
        String right = right_num.getText().toString().toUpperCase();
        if(right.contains("I")||right.contains("O")||right.length()!=5){
            toast("请检查您的车牌号是否正确");
            return;
        }
        if(number.length()!=7){
            toast("请检查您的车牌号是否正确");
            return;
        }
        carRequest = new HttpCarRequest(this,0);
        carRequest.addCarNum(number);
        carRequest.setmIHttpClinetListener(this);
        carRequest.doRequest();
    }
    public void changeLeft(View view){
        BaseTools.hideSoftInput(this);
        if(mStringPop==null){
            mStringPop = new PopupWindow(getLayoutInflater().inflate(R.layout.pop_add_num,null), WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.WRAP_CONTENT);
            // 设置popWindow弹出窗体可点击，这句话必须添加，并且是true
            mStringPop.setAnimationStyle(R.style.pop_from_bottom);
        }
        if(mStringPop!=null&&mStringPop.isShowing()){
            mStringPop.dismiss();
        }else{
            mStringPop.showAtLocation(activity_ll,Gravity.BOTTOM,0,0);
        }
    }

    /**
     * 中文选择
     * @param view
     */
    public void selectString(View view){
        BaseTools.hideSoftInput(this);
        if(mStringPop!=null){
            mStringPop.dismiss();
        }
        String lef = ((TextView)view).getText().toString();
        if(BaseTools.isValueEmpty(lef)){
            return;
        }
        first = lef;
        left_num.setText(lef+"A");
        if(mCharPop==null){
            mCharPop = new PopupWindow(getLayoutInflater().inflate(R.layout.pop_add_char,null), WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.WRAP_CONTENT);
            // 设置popWindow弹出窗体可点击，这句话必须添加，并且是true
            mCharPop.setFocusable(true);
            mCharPop.setAnimationStyle(R.style.pop_from_bottom);
        }
        if(mCharPop!=null&&mCharPop.isShowing()){
            mCharPop.dismiss();
        }else{
            mCharPop.showAtLocation(activity_ll, Gravity.BOTTOM,0,0);
        }
    }

    /**
     * 字母选择
     * @param view
     */
    public void selectChar(View view){
        if(mCharPop!=null){
            mCharPop.dismiss();
        }
        String lef = ((TextView)view).getText().toString();
        if(BaseTools.isValueEmpty(lef)){
            return;
        }

        left_num.setText(first+lef);
    }

    @Override
    public void httpSuccessListener(Bundle successMsg) {
        super.httpSuccessListener(successMsg);
        if(carRequest!=null&&action.equalsIgnoreCase(carRequest.getAction())){
            if(mBaseResult!=null&&mBaseResult.getStatus().getRetCode()==0){
                setResult(Constrants.ADD_CAR);
                finish();
            }
             toast(mBaseResult.getStatus().getMsg());

        }
    }
}
